package com.zidian.teacher.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.zidian.teacher.R;
import com.zidian.teacher.bean.WorkListBean;

import java.util.List;



/**
 * Created by Administrator on 2016/8/24 0024.
 */
public class TeachingWorkAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;
    private static final int TYPE_TOP = 2;
    private Context context;
    private List<WorkListBean.DataBean> data;
    private static boolean hasMore = true;


    public TeachingWorkAdapter(Context context, List<WorkListBean.DataBean> data) {
        this.context = context;
        this.data = data;
    }

    public void setMore(boolean hasMore) {
        this.hasMore = hasMore;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnSearchClick {
        void onSearchClick(LinearLayout searchLin, LinearLayout searchEditLin, EditText edit);
    }

    private OnSearchClick onSearchClick;

    public void setOnSearchClick(OnSearchClick onSearchClick) {
        this.onSearchClick = onSearchClick;
    }

    @Override
    public int getItemCount() {
        return data.size() == 0 ? 1 : data.size() + 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_TOP;
        } else if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_teaching_work, parent,
                    false);
            return new ItemViewHolder(view);
        } else if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_foot, parent,
                    false);
            return new FootViewHolder(view);
        } else if (viewType == TYPE_TOP) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_search_top, parent,
                    false);
            return new TopViewHolder(view);
        }
        return null;
    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {

            ((ItemViewHolder) holder).className.setText(data.get(position - 1).getWorkName());
            ((ItemViewHolder) holder).startTime.setText(data.get(position - 1).getReleaseTime());
            ((ItemViewHolder) holder).endTime.setText(data.get(position - 1).getHandoverTime());
            ((ItemViewHolder) holder).content.setText(data.get(position - 1).getSchoolWorkContent());


            if (onItemClickListener != null) {
            }
        } else if (holder instanceof TopViewHolder) {
            ((TopViewHolder) holder).tv.setText("搜索你要找的作业");

            ((TopViewHolder) holder).seachTextLin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((TopViewHolder) holder).seachTextLin.setVisibility(View.GONE);
                    ((TopViewHolder) holder).seachEditLin.setVisibility(View.VISIBLE);
                }
            });

            if (onSearchClick != null) {

                ((TopViewHolder) holder).searchBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((TopViewHolder) holder).seachTextLin.setVisibility(View.VISIBLE);
                        ((TopViewHolder) holder).seachEditLin.setVisibility(View.GONE);
                        onSearchClick.onSearchClick(((TopViewHolder) holder).seachTextLin, ((TopViewHolder) holder).seachEditLin, ((TopViewHolder) holder).searchEdit);
                    }
                });

            }


        } else if (holder instanceof FootViewHolder) {
            if (!hasMore) {
                ((FootViewHolder) holder).loadText.setText("无数据可以加载!");
                ((FootViewHolder) holder).progressBar.setVisibility(View.GONE);
            } else {
                ((FootViewHolder) holder).loadText.setText("正在加载...");
                ((FootViewHolder) holder).progressBar.setVisibility(View.VISIBLE);
            }

        }
    }


    static class ItemViewHolder extends RecyclerView.ViewHolder {

        TextView className;
        TextView startTime;
        TextView endTime;
        TextView content;

        public ItemViewHolder(View view) {
            super(view);
            className = (TextView) view.findViewById(R.id.work_class_name);
            startTime = (TextView) view.findViewById(R.id.work_begin_time);
            endTime = (TextView) view.findViewById(R.id.work_end_time);
            content = (TextView) view.findViewById(R.id.work_content);

        }
    }

    static class FootViewHolder extends RecyclerView.ViewHolder {
        TextView loadText;
        ProgressBar progressBar;

        public FootViewHolder(View view) {
            super(view);
            loadText = (TextView) view.findViewById(R.id.load_text);
            progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
        }
    }

    static class TopViewHolder extends RecyclerView.ViewHolder {
        TextView tv;
        LinearLayout seachTextLin;
        LinearLayout seachEditLin;
        LinearLayout searchLin;
        Button searchBtn;
        EditText searchEdit;

        public TopViewHolder(View view) {
            super(view);
            tv = (TextView) view.findViewById(R.id.hint_name);
            seachTextLin = (LinearLayout) view.findViewById(R.id.searchLinear);
            seachEditLin = (LinearLayout) view.findViewById(R.id.searchEditLinear);
            searchLin = (LinearLayout) view.findViewById(R.id.searchAndCountLinear);
            searchBtn = (Button) view.findViewById(R.id.search_btn);
            searchEdit = (EditText) view.findViewById(R.id.search_edit);
        }
    }

}
